package com.sly.demoim.util;

import lombok.extern.slf4j.Slf4j;
import sun.net.util.IPAddressUtil;

import java.lang.reflect.Method;
import java.net.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
 * IP工具类
 * @author SLY
 * @date 2022/7/17
 */
@Slf4j
public class IpUtil {
    /** linux 王卡名 */
    private static final String LINUX_NI_NAME = "eth0";
    /** 操作系统名称 KEY */
    private static final String OS_NAME = "os.name";
    /** 操作系统名称 */
    private static final List<String> OS_NAME_LIST = Collections.unmodifiableList(Arrays.asList("Windows", "Linux"));

    /**
     * 获取服务器IP地址
     *
     * @return {@link String}
     * @author SLY
     * @date 2022/7/17
     */
    public static String getLocalIp() {
        try {
            String osName = getOsName();
            Method method = IpUtil.class.getDeclaredMethod("get" + osName + "LocalIp");
            method.setAccessible(true);
            return (String) method.invoke(null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取Linux系统本机IP
     *
     * @return {@link String} IP地址
     * @author SLY
     * @date 2022/7/17
     */
    private static String getLinuxLocalIp() {
        Enumeration<NetworkInterface> allNetworkInterfaces;
        try {
            allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            throw new RuntimeException("获取Linux系统本机IP异常", e);
        }

        if(allNetworkInterfaces != null){
            while (allNetworkInterfaces.hasMoreElements()){
                NetworkInterface networkInterface = allNetworkInterfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if(networkInterface.getName().equals(LINUX_NI_NAME)){
                        if(address instanceof Inet4Address){
                            return address.getHostAddress();
                        }
                    }
                }
            }
        }

        throw new RuntimeException("未获取到linux服务器IP");
    }

    /**
     * 获取Windows系统本机IP
     *
     * @return {@link String} IP地址
     * @author SLY
     * @date 2022/7/17
     */
    private static String getWindowsLocalIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException("获取Windows系统本机IP异常", e);
        }
    }

    /**
     * 获取当前操作系统名称
     *
     * @return {@link String} 操作系统名称
     * @author SLY
     * @date 2022/7/17
     */
    private static String getOsName() {
        String originOsName = System.getProperties().getProperty(OS_NAME);
        String originOsNameUpper = originOsName.toUpperCase();
        for (String osName : OS_NAME_LIST) {
            if (originOsNameUpper.contains(osName.toUpperCase())) {
                return osName;
            }
        }
        throw new RuntimeException("不支持操作系统：" + originOsName);
    }
}
